<?php

namespace App\Http\Controllers\Admin;

use App\Models\JifenProduct;
use App\Models\ProdCategory;
use App\Models\ProdDiscount;
use App\Models\Product;
use Illuminate\Http\Request;

/**
 * @group 后台接口文档
 * @title 商品管理模块接口
 * @author chenke
 * @description 酒庄后台商品管理板块接口
 * @header name:X-Token type:string require:1 desc:登录令牌
 *  */
class Prod extends Base
{
    /**
     * @title 商品分类列表
     * @description 后台商品分类列表数据
     * @url /admin/prod/cateshow
     * @method GET
     * @module 商品分类板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对应分类英文名)
     * @return code:错误码
     * @return message:错误提示
     * @return data:商品分类信息@
     * @data id:分类ID catename:商品英文名 catename_zn:中文名
     */
    public function cateList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $where = [];
        if($searchCont){
            $where[] = ['catename','like','%'.$searchCont.'%'];
        }
        $return = (new ProdCategory())->getPageList(['limit'=>$limit,'page'=>$page],$where);
        return response()->json($return);
    }

    /**
     * @title 商品分类添加
     * @description 添加单个商品分类
     * @url /admin/prod/crecate
     * @method POST
     * @module 商品分类板块
     * @author chenke
     * @param name:catename type:string require:1 default: other: desc:商品分类英文名
     * @param name:catename_zn type:string require:1 default: other: desc:商品分类中文名
     * @return code:错误码
     * @return message:错误提示
     * @return data: 商品分类ID
     */
    public function cateAdd(Request $request){
        $data = $request->post();
        $rules = [
            'catename'=>'required|string',
            'catename_zn'=>'required|string',
        ];
        $messages = [
            'catename.required'=> '请填写英文名',
            'catename_zn.required'=> '请填写中文名'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new ProdCategory())->oneInsert($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'cateAdd');
        }
        return response()->json($result);
    }

    /**
     * @title 商品分类编辑
     * @description 编辑单个商品分类
     * @url /admin/prod/edtcate
     * @method POST
     * @module 商品分类板块
     * @author chenke
     * @param name:id type:int require:1 default:0 other: desc:商品分类ID
     * @param name:catename type:string require:1 default: other: desc:商品分类英文名
     * @param name:catename_zn type:string require:1 default: other: desc:商品分类中文名
     * @return code:错误码
     * @return message:错误提示
     * @return data:商品分类ID
     */
    public function cateUpdate(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'catename'=>'required|string',
            'catename_zn'=>'required|string'

        ];
        $messages = [
            'id.required'=> '请选择商品分类',
            'id.integer'=> 'ID错误',
            'catename.required'=> '请填写英文名',
            'catename_zn.required'=> '请填写中文名'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new ProdCategory())->doUpdate($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodUpdate');
        }
        return response()->json($result);
    }

    /**
     * @title 商品列表
     * @description 后台商品列表数据
     * @url /admin/prod/prodshow
     * @method GET
     * @module 商品板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对应商品英文名)
     * @param name:status type:int require:0 default: other: desc:搜索状态(0全部 1上架 2下架)
     * @param name:startDate type:date require:0 default: other: desc:搜索日期（开始日期）
     * @param name:endDate type:date require:0 default: other: desc:搜索日期（结束日期）
     * @return code:错误码
     * @return message:错误提示
     * @return data:商品数据@
     * @data id:商品ID cids:商品分类ID,多个以半圆角逗号隔开 catenames:所属分类中文名 proname:商品英文名 proname_zn:中文名 image:商品主图 mprice:商品价格 rprice:活动价格 salenum:商品销量 content:商品详情
     */
    public function prodList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:'';
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:'';
        $status = (isset($data['status']) && $data['status'])?$data['status']:0;
        $where = [];
        if($searchCont){
            $where[] = ['proname','like','%'.$searchCont.'%'];
        }
        if($status){
            if($status ==1 ){
                $where[] = ['deleted_at','=',null];
            }else{
                $where[] = ['deleted_at','<>',null];
            }
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $return = (new Product())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($return);
    }

    /**
     * @title 商品添加
     * @description 添加单个商品数据
     * @url /admin/prod/creprod
     * @method POST
     * @module 商品板块
     * @author chenke
     * @param name:cids type:string require:1 default: other: desc:商品分类ID,多个分类以半圆角逗号隔开
     * @param name:image type:string require:1 default: other: desc:商品主图
     * @param name:proname type:string require:1 default: other: desc:商品英文名
     * @param name:proname_zn type:string require:1 default: other: desc:商品中文名
     * @param name:mprice type:int require:1 default:0 other: desc:商品价格
     * @param name:rprice type:int require:1 default:0 other: desc:商品活动价格
     * @param name:salenum type:int require:1 default:0 other: desc:商品销量
     * @param name:content type:string require:1 default: other: desc:商品详情
     * @return code:错误码
     * @return message:错误提示
     * @return data: 商品ID
     */
    public function prodAdd(Request $request){
        $data = $request->post();
        $rules = [
            'cids'=>'required|string',
            'proname'=>'required|string',
            'proname_zn'=>'required|string',
            'image'=>'required|string',
            'mprice'=>'required|integer',
            'rprice'=>'required|integer',
            'salenum'=>'required|integer',
            'content'=>'required|string',

        ];
        $messages = [
            'cids.required'=> '请选择商品分类',
            'proname.required'=> '请填写英文名',
            'proname_zn.required'=> '请填写中文名',
            'image.required'=> '请上传主图',
            'mprice.required'=> '请填写商品价格',
            'rprice.required'=> '请填写活动价格',
            'salenum.required'=> '请填写销量',
            'mprice.integer'=> '商品价格必须是数字',
            'rprice.integer'=> '活动价格必须是数字',
            'salenum.integer'=> '销量必须是数字',
            'content.required'=> '请填写商品详情'
        ];

        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Product())->oneInsert($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodAdd');
        }
        return response()->json($result);
    }

    /**
     * @title 商品编辑
     * @description 编辑单个商品数据
     * @url /admin/prod/edtprod
     * @method POST
     * @module 商品板块
     * @author chenke
     * @param name:id type:int require:1 default:0 other: desc:用户ID
     * @param name:cids type:string require:1 default: other: desc:商品分类ID,多个分类以半圆角逗号隔开
     * @param name:image type:string require:1 default: other: desc:商品主图
     * @param name:proname type:string require:1 default: other: desc:商品英文名
     * @param name:proname_zn type:string require:1 default: other: desc:商品中文名
     * @param name:mprice type:int require:1 default:0 other: desc:商品价格
     * @param name:rprice type:int require:1 default:0 other: desc:商品活动价格
     * @param name:salenum type:int require:1 default:0 other: desc:商品销量
     * @param name:content type:string require:1 default: other: desc:商品详情
     * @return code:错误码
     * @return message:错误提示
     * @return data:商品ID
     */
    public function prodUpdate(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'cids'=>'required|string',
            'proname'=>'required|string',
            'proname_zn'=>'required|string',
            'image'=>'required|string',
            'mprice'=>'required|integer',
            'rprice'=>'required|integer',
            'salenum'=>'required|integer',
            'content'=>'required|string',

        ];
        $messages = [
            'id.required'=> '请选择商品',
            'id.integer'=> '商品ID错误',
            'cids.required'=> '请选择商品分类',
            'proname.required'=> '请填写英文名',
            'proname_zn.required'=> '请填写中文名',
            'image.required'=> '请上传主图',
            'mprice.required'=> '请填写商品价格',
            'rprice.required'=> '请填写活动价格',
            'salenum.required'=> '请填写销量',
            'mprice.integer'=> '商品价格必须是数字',
            'rprice.integer'=> '活动价格必须是数字',
            'salenum.integer'=> '销量必须是数字',
            'content.required'=> '请填写商品详情'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Product())->doUpdate($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodUpdate');
        }
        return response()->json($result);
    }

    /**
     * @title 商品状态改变
     * @description 上架/下架 商品
     * @url /admin/prod/prodstatus
     * @method POST
     * @module 商品板块
     * @author chenke
     * @param name:id type:int require:1 default:0 other: desc:用户ID
     * @param name:status type:int require:1 default:0 other: desc:用户要变成的状态1为正常0为禁用
     * @return code:错误码
     * @return message:错误提示
     * @return data:用户ID
     */
    public function changeProd(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'status'=>'required|integer'
        ];
        $messages = [
            'id.required'=> '商品ID不存在',
            'id.integer'=> '商品ID参数错误',
            'status.required'=> '状态参数不存在',
            'status.integer'=> '状态参数错误',
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $model = new Product();
        $result = $model->changeStatus($request->post());
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'changeProd');
        }
        return response()->json($result);
    }

    /**
     * @title 商品等级折扣设置
     * @description 某个商品不同会员等级设置不同的折扣
     * @url /admin/prod/setprod
     * @method POST
     * @module 商品板块
     * @author chenke
     * @param name:proid type:int require:1 default:0 other: desc:商品ID
     * @param name:discounts type:json require:1 default:{} other: desc:折扣设置数据--如{1:90,2:80,3:70,4:60}代表:等级ID为1,2,3,4的折扣值分别为90,80,70,60
     * @return code:错误码
     * @return message:错误提示
     * @return data:正确时为空
     */
    public function prodSetting(Request $request){
        $data = $request->post();
        $rules = [
            'proid'=>'required|integer',
            'discounts'=>'required|json'
        ];
        $messages = [
            'proid.required'=> '请选择商品',
            'proid.integer'=> '商品ID错误',
            'discounts.required'=> '请设置折扣',
            'discounts.json'=> '折扣数据格式错误'
        ];
//        return response()->json($data['discounts']);
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Product())->setDiscount($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodSetting');
        }
        return response()->json($result);
    }

    /**
     * @title 获取商品所有等级折扣
     * @description 某个商品不同会员等级设置不同的折扣
     * @url /admin/prod/prosets
     * @method GET
     * @module 商品板块
     * @author chenke
     * @param name:id type:int require:1 default:0 other: desc:商品ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:设置数据@
     * @data  用户等级ID:对应级别的折扣值
     */
    public function prodSets(Request $request){
        try {
            $id = $request->get('id');
            if(!$id){
                return response()->json([
                    'code'=>1,
                    'message'=>'请传商品ID参数'
                ]);
            }
            $result =  (new ProdDiscount())->where('proid',$id)->select('levid','discount')->get();
            $data = [];
            if(!empty($result)){
                foreach ($result as $res){
                    $data[$res->levid] = $res->discount;
                }
            }
            return response()->json([
                'code'=>0,
                'data'=>$data
            ]);
        }catch (\Exception $e){
            $this->logErr($e->getMessage(),$e->getMessage(),'prodSets');
            return response()->json([
                'code'=>1,
                'message'=>'获取失败'
            ]);
        }
    }

    /**
     * @title 商品图片设置
     * @description 设置商品详情页的轮播商品图
     * @url /admin/prod/setimgs
     * @method POST
     * @module 商品板块
     * @author chenke
     * @param name:proid type:int require:1 default:0 other: desc:商品ID
     * @param name:images type:array require:1 default:[] other: desc:图片地址组
     * @return code:错误码
     * @return message:错误提示
     * @return data:正确时为空
     */
    public function prodImages(Request $request){
        $data = $request->post();
        $rules = [
            'proid'=>'required|integer',
            'images'=>'required|array'
        ];
        $messages = [
            'proid.required'=> '请选择商品',
            'proid.integer'=> '商品ID错误',
            'images.required'=> '请上传至少一张图片',
            'images.array'=> '图片数据格式错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Product())->setProdImgs($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodImages');
        }
        return response()->json($result);
    }

    /**
     * @title 积分商品列表
     * @description 后台商品分类列表数据
     * @url /admin/prod/jproshow
     * @method GET
     * @module 积分商品板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对应商品英文名)
     * @return code:错误码
     * @return message:错误提示
     * @return data:积分商品信息@
     * @data id:商品ID proname:商品英文名 proname_zn:中文名 image:商品主图 price:商品价格 coin:兑换所需积分 salenum:已兑换数量 deleted_at:是否删除
     */
    public function jifenProdList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $where = [];
        if($searchCont){
            $where[] = ['proname','like','%'.$searchCont.'%'];
        }
        $return = (new JifenProduct())->getPageList(['limit'=>$limit,'page'=>$page],$where);
        return response()->json($return);
    }

    /**
     * @title 积分商品添加
     * @description 添加单个积分商品
     * @url /admin/prod/jprocre
     * @method POST
     * @module 积分商品板块
     * @author chenke
     * @param name:image type:string require:1 default: other: desc:商品主图
     * @param name:proname type:string require:1 default: other: desc:商品英文名
     * @param name:proname_zn type:string require:1 default: other: desc:商品中文名
     * @param name:price type:int require:1 default:0 other: desc:商品价格
     * @param name:coin type:int require:1 default:0 other: desc:兑换所需积分
     * @param name:salenum type:int require:1 default:0 other: desc:商品销量
     * @param name:content type:string require:1 default: other: desc:商品详情
     * @return code:错误码
     * @return message:错误提示
     * @return data: 商品ID
     */
    public function jifenProdAdd(Request $request){
        $data = $request->post();
        $rules = [
            'proname'=>'required|string',
            'proname_zn'=>'required|string',
            'image'=>'required|string',
            'price'=>'required|integer',
            'coin'=>'required|integer',
            'salenum'=>'required|integer',
            'content'=>'required|string',

        ];
        $messages = [
            'proname.required'=> '请填写英文名',
            'proname_zn.required'=> '请填写中文名',
            'image.required'=> '请上传主图',
            'price.required'=> '请填写商品价格',
            'coin.required'=> '请填写兑换所需积分',
            'salenum.required'=> '请填写销量',
            'price.integer'=> '商品价格必须是数字',
            'coin.integer'=> '积分必须是数字',
            'salenum.integer'=> '销量必须是数字',
            'content.required'=> '请填写商品详情'
        ];

        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new JifenProduct())->oneInsert($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'jifenProdAdd');
        }
        return response()->json($result);
    }

    /**
     * @title 积分商品编辑
     * @description 编辑单个积分商品
     * @url /admin/prod/jproedt
     * @method POST
     * @module 积分商品板块
     * @author chenke
     * @param name:image type:string require:1 default: other: desc:商品主图
     * @param name:proname type:string require:1 default: other: desc:商品英文名
     * @param name:proname_zn type:string require:1 default: other: desc:商品中文名
     * @param name:price type:int require:1 default:0 other: desc:商品价格
     * @param name:coin type:int require:1 default:0 other: desc:兑换所需积分
     * @param name:salenum type:int require:1 default:0 other: desc:商品销量
     * @param name:content type:string require:1 default: other: desc:商品详情
     * @return code:错误码
     * @return message:错误提示
     * @return data: 商品ID
     */
    public function jifenProdUpdate(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'proname'=>'required|string',
            'proname_zn'=>'required|string',
            'image'=>'required|string',
            'price'=>'required|integer',
            'coin'=>'required|integer',
            'salenum'=>'required|integer',
            'content'=>'required|string',

        ];
        $messages = [
            'id.required'=> '商品ID不存在',
            'proname.required'=> '请填写英文名',
            'proname_zn.required'=> '请填写中文名',
            'image.required'=> '请上传主图',
            'price.required'=> '请填写商品价格',
            'coin.required'=> '请填写兑换所需积分',
            'salenum.required'=> '请填写销量',
            'price.integer'=> '商品价格必须是数字',
            'coin.integer'=> '积分必须是数字',
            'id.integer'=> 'ID错误',
            'salenum.integer'=> '销量必须是数字',
            'content.required'=> '请填写商品详情'
        ];

        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new JifenProduct())->doUpdate($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'jifenProdUpdate');
        }
        return response()->json($result);
    }

    /**
     * @title 积分商品状态改变
     * @description 上架/下架 积分商品
     * @url /admin/prod/jproset
     * @method POST
     * @module 积分商品板块
     * @author chenke
     * @param name:id type:int require:1 default:0 other: desc:用户ID
     * @param name:status type:int require:1 default:0 other: desc:用户要变成的状态1为正常0为禁用
     * @return code:错误码
     * @return message:错误提示
     * @return data:用户ID
     */
    public function changeJifenProd(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'status'=>'required|integer'
        ];
        $messages = [
            'id.required'=> '商品ID不存在',
            'id.integer'=> '商品ID参数错误',
            'status.required'=> '状态参数不存在',
            'status.integer'=> '状态参数错误',
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $model = new JifenProduct();
        $result = $model->changeStatus($request->post());
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'changeJifenProd');
        }
        return response()->json($result);
    }

    /**
     * @title 积分商品图片设置
     * @description 设置积分商品详情页的轮播商品图
     * @url /admin/prod/jfenimgs
     * @method POST
     * @module 积分商品板块
     * @author chenke
     * @param name:proid type:int require:1 default:0 other: desc:商品ID
     * @param name:images type:array require:1 default:[] other: desc:图片地址组
     * @return code:错误码
     * @return message:错误提示
     * @return data:正确时为空
     */
    public function jifenProdImages(Request $request){
        $data = $request->post();
        $rules = [
            'proid'=>'required|integer',
            'images'=>'required|array'
        ];
        $messages = [
            'proid.required'=> '请选择商品',
            'proid.integer'=> '商品ID错误',
            'images.required'=> '请上传至少一张图片',
            'images.array'=> '图片数据格式错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new JifenProduct())->setProdImgs($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodImages');
        }
        return response()->json($result);
    }

}
